home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1360 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  58 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: Re: Problem with sscanf on DEC Alpha
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <DL37x0.Jo4@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: schmorganization
  9. References: <4d3f0c$mfq@colossus.holonet.net>
  10. Date: Fri, 12 Jan 1996 21:34:11 GMT
  11.  
  12. In article <4d3f0c$mfq@colossus.holonet.net>, mitch@news.mdli.com
  13. (Mitch Miller) writes:
  14. > I'm having a problem with a line in my program:
  15. >   sscanf( sInputLine, "%3d%3d%10.4f%10.4f", &iAtom1, 
  16. >     &iAtom2, &(F3D->r3DV1), &(F3D->r3DV2) );
  17. > F3D is a pointer to struct and the struct has double members
  18. > r3DV1 and r3DV2.  
  19. >
  20. > When I've compiled/run this on a VAX, both F3D->r3DV1 and F3D->r3DV2 
  21. > get assigned correctly 9.4 and 10.5.  However, on the Alpha, both
  22. > end up as 0.0.
  23.  
  24. It works on a VAX because the VAX uses what I call "little endian
  25. floating point": a float value is a proper subset of a double; if
  26. you take a pointer to a double value, and look at the first
  27. sizeof(float) bytes there, they represent a valid float value.
  28. This is, however, not at all universal, and on most machines, if
  29. you're careless about pointer-to-float vs. pointer-to-double,
  30. things don't work to well.
  31.  
  32. How are pointers-to-float involved in your problem?  They're what
  33. scanf's %f format expects.  The comp.lang.c FAQ list explains why;
  34. here's the text from the book-length version (Addison-Wesley,
  35. 1996, ISBN 0-201-84519-9):
  36.  
  37. 12.13:    Why doesn't this code:
  38.  
  39.         double d;
  40.         scanf("%f", &d);
  41.  
  42.     work?
  43.  
  44. A:    Unlike printf(), scanf() uses %lf for values of type double, and
  45.     %f for float. [1]  %f tells scanf() to expect a pointer-to-
  46.     float, not the pointer-to-double you gave it.  Either use %lf,
  47.     or declare the receiving variable as a float.  See also question
  48.     12.9.  
  49.  
  50. __________
  51. 1.  Everything said here is equally true of %e and %g, and their
  52.     companion formats %le and %lg.
  53.  
  54.                         Steve Summit
  55.                         scs@eskimo.com
  56.